/**
 *
 */
package com.zhuoyue.elmt.service;

import java.util.HashMap;
import java.util.List;

import com.zhuoyue.elmt.Constants;
import com.zhuoyue.elmt.entity.MaintenanceRecordSz;
import com.zhuoyue.elmt.entity.WbFaultComplaints;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.service.CrudService;
import com.zhuoyue.elmt.entity.FailureReport;
import com.zhuoyue.elmt.dao.FailureReportDao;

/**
 * 电梯故障报告Service
 *
 * @author chenhao
 * @version 2016-09-07
 */
@Service
@Transactional(readOnly = true)
public class FailureReportService extends CrudService<FailureReportDao, FailureReport> {

    @Autowired
    private WbFaultComplaintsService wbFaultComplaintsService;

    public FailureReport get(String id) {
        return super.get(id);
    }

    public List<FailureReport> findList(FailureReport failureReport) {
        return super.findList(failureReport);
    }

    public Page<FailureReport> findPage(Page<FailureReport> page, FailureReport failureReport) {
        return super.findPage(page, failureReport);
    }

    @Transactional(readOnly = false)
    public void save(FailureReport failureReport) {
        super.save(failureReport);

        rewritefault(failureReport);
    }

    @Transactional(readOnly = false)
    public void delete(FailureReport failureReport) {
        super.delete(failureReport);
    }

    public FailureReport getFailureReportByDeviceCode(HashMap params) {
        return dao.getFailureReportByDeviceCode(params);
    }

    @Transactional(readOnly = false)
    public void deleteFailureReportInfo(FailureReport record) {
        //删除配件更换记录
        dao.deleteFailureReplacementPartByReportId(record.getFormId());
        //删除配件更换记录
        dao.deleteFailureSurveyContentByReportId(record.getFormId());
        //删除故障报告
        dao.delete(record);
    }

    public FailureReport getByFailureNo(FailureReport failureReport) {
        return dao.getByFailureNo(failureReport);
    }

    public void rewritefault(FailureReport failureReport) {
        // 遇过来源不为空 并且是faultNo 反写wb_fault_complaints
        if (StringUtils.isNotEmpty(failureReport.getSourceNo()) && Constants.FailureReport.SOURCE_TYPE_FAULT.equals(failureReport.getSourceType())) {
            WbFaultComplaints wbFaultComplaints = wbFaultComplaintsService.getByFaultNo(failureReport.getSourceNo());
            wbFaultComplaints.setFailureNo(failureReport.getFailureNo());
            wbFaultComplaints.setFaultState(Constants.WbFaultComplaints.FAULT_STATE_COMPLETE);
            wbFaultComplaintsService.update(wbFaultComplaints);
        }
    }

}